<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GoodsController extends Controller
{


  public function getIndex(Request $request){
     if(empty(session('id'))){
        return redirect('/admin')->with('error','请您登陆');
      }
       //  获取商家数据
        $users = DB::table('users') ->where('id',$request -> input('id'))-> first();
        $id = $users['id'];
        session(['id'=>$id]);

        $username = $users['username'];
       
          // 分页
        $count = $request -> input('count',10);
       // 关键字
        $search = $request -> input('search','');

        // 对查询语句构造器进行分页  paginate  查询数据并且分页
        // 获取商家相关商品
        $data = DB::table('goods') ->where('name','like','%'.$search.'%') -> where('pid',$users['id']) -> paginate($count);

        return view('admin/goods/index ',['data'=>$data,'count'=>$count,'search'=>$search,'request'=>$request->all(),'username'=>$username,'id'=>$id]);
 
  }
  // 商品添加页
  public function getAdd(){
    $data = DB::table('pic') -> get();
    return view('admin/goods/add',['data'=>$data]);
  }

  // 商品添加
  public function postInsert(Request $request){

    // 获取商家id 
    $pid = $request -> input('id');
    // 获取表单数据 进行处理
   $arr = $request -> only('name','title','content','price','express','model','memory','locations','status','lid','color');
   // dd($arr);
    // $arr = $request ->except('_token','id','gpic','xpic','xxpic','xxxpic');
   // dd($arr);
    $arr['createtime'] = date('Y-m-d H:i:s',time());
    $arr['pid'] = $pid;
    // dd($arr);
      if($request -> hasFile('gpic')){

            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('gpic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('gpic') -> move('./upload',$tname);
          $arr['gpic'] = $tname; 
        }
         if($request -> hasFile('xpic')){

            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('xpic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('xpic') -> move('./upload',$tname);
          $arr['xpic'] = $tname; 
        }
         if($request -> hasFile('xxpic')){

            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('xxpic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('xxpic') -> move('./upload',$tname);
          $arr['xxpic'] = $tname; 
        }
      
    $res = DB::table('goods') ->insert($arr);
  if($res){
    return redirect('/admin/goods/index?id='.$pid) ->withInput()-> with('success','添加成功');
  }else{
    return back() -> with('error','添加失败');
  }

  }
// 删除商品
  public function getDelete(Request $request){
         $pid = $request -> input('pid');
        $id = $request -> input('id');
    
       $data = DB::table('goods') ->where('id',$id)-> delete();
       if($data){
        return redirect('/admin/goods/index?id='.$pid)->with('success','删除成功');
       }else{
        return back() -> with('error','删除失败');
       }
  }
  // 修改商品页
  public function getEdit(Request $request){
    // 接受id 
    $id = $request -> input('id');
   
    $data = DB::table('goods') ->where('id',$id)-> first();
    // dd($data);
     $pic = DB::table('pic') -> get();

    return view('admin/goods/edit',['data'=>$data,'pic'=>$pic]);
  }
  //修改
  public function postUpdate(Request $request){
    // dd($_POST);
    $res = $request -> except('_token','gpic','xpic','xxpic');
    // dd($res);
    $data = DB::table('goods') ->where('id',$res['id']) -> first();
    
   $pid = $data['pid'];
     if($request -> hasFile('gpic')){

            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('gpic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('gpic') -> move('./upload',$tname);
          $res['gpic'] = $tname; 
        }


         if($request -> hasFile('xpic')){

            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('xpic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('xpic') -> move('./upload',$tname);
          $res['xpic'] = $tname; 
        }


         if($request -> hasFile('xxpic')){

            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('xxpic') -> getClientOriginalExtension();
            // 拼接文件名
            $tname = $temp_name.'.'.$hz;
            $request -> file('xxpic') -> move('./upload',$tname);
          $res['xxpic'] = $tname; 
        }
   // dd($res);
   
    // 进行修改
    $data1 = DB::table('goods') -> where('id',$res['id']) -> update($res);
    if($data = 1){
        return redirect('/admin/goods/index?id='.$pid) -> with('success','修改成功');
    }else{
        return back() -> with('error','修改失败');
    }
  } 



}
